//
//  QNuploader.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/12.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "QNuploader.h"
#import <QiniuSDK.h>
#import <UIKit/UIKit.h>
#import "oldNetWork.h"
#import "oldHub.h"

@interface  QNuploader()

@property (nonatomic, strong)NSString *token;
@property (nonatomic, strong)NSString *voiceToken;
@property (nonatomic, strong)NSString *videoToken;
@property (nonatomic, strong)QNUploadManager *upManager;

@end

@implementation QNuploader

+ ( instancetype ) shareInstance
{
    static QNuploader * instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once( &onceToken, ^
                  {
                      instance = [self new];
                  });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self getQiNiuToken];
        QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
            builder.zone = [QNFixedZone zone2];
        }];
        QNUploadManager *upManager = [[QNUploadManager alloc] initWithConfiguration:config];
        self.upManager = upManager;
    }
    
    return self;
}

- (void)getQiNiuToken
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_QiNiu_Token params:@{ @"type" : @"pic" } success:^(NSDictionary *dic) {
        weakSelf.token = dic[@"token"];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
    
    [OLDNetWork httpRequestWithPostPut:URL_QiNiu_Token params:@{ @"type" : @"voice" } success:^(NSDictionary *dic) {
        weakSelf.voiceToken = dic[@"token"];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
    
    [OLDNetWork httpRequestWithPostPut:URL_QiNiu_Token params:@{ @"type" : @"video" } success:^(NSDictionary *dic) {
        weakSelf.videoToken = dic[@"token"];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (NSString *)setTokenForUrl:(NSString *)url type:(QiNiuUploadType)type
{
    NSString *token = nil;
    if (type == Voice_file) {
        token = self.voiceToken;
    }
    else if(type == Video_file)
    {
        token = self.videoToken;
    }
    else
    {
        token = self.token;
    }
    
    return [NSString stringWithFormat:@"%@?token=%@", url, token];
}

- (void)uploadImageToQNFilePath:(NSString *)filePath type:(QiNiuUploadType)type block:(QNuploaderBlock)block
{
    NSString *token = nil;
    NSString *mime = nil;
    if (type == Voice_file) {
        token = self.voiceToken;
//        mime = @"abc.mp3";
    }
    else if(type == Video_file)
    {
        token = self.videoToken;
    }
    else
    {
        token = self.token;
    }
    
    if (!token) {
        [self getQiNiuToken];
    }
    
    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
        NSLog(@"percent == %.2f", percent);
    }
                                                                 params:nil
                                                               checkCrc:NO
                                                     cancellationSignal:nil];
    [_upManager putFile:filePath key:mime token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        NSLog(@"info ===== %@", info);
        NSLog(@"resp ===== %@", resp);
        block(resp[@"key"]);
    }
                option:uploadOption];
}

@end
